<div class="row-fluid">
    <div class="span6">
        <div class="portlet box green">
            <div class="portlet-title">
                <div class="caption"><i class="icon-reorder"></i>Үлдэгдэл</div>
                <div class="tools">
                    <a href="javascript:;" class="collapse"></a>
                </div>
            </div>
            <div class="portlet-body">
                <div class="widget-box">
                    <?php if ($rows): ?>
                        <table id="inventory_items" class="table table-striped table-bordered table-hover">
                            <thead>
                                <tr>
                                    <th></th>
                                    <th>Ангилал</th>
                                    <th>Бараа</th>
                                    <th>Үлдэгдэл</th>
                                </tr>
                            </thead>
                            <tbody>
                                <?php $count = 0; ?>
                                <?php $cat = 0; ?>
                                <?php $catTotal = 0; ?>
                                <?php foreach ($rows AS $row): ?>
                                    <?php if ($cat != $row['category_id']): ?>
                                        <?php $cat = $row['category_id'] ?>
                                        <?php $i = 0; ?>
                                        <?php if ($count): ?>
                                            <tr>
                                                <td colspan="3"></td>
                                                <td><?php echo $catTotal ?></td>
                                            </tr>
                                        <?php endif; ?>
                                        <?php $catTotal = 0; ?>
                                    <?php endif; ?>
                                    <tr>
                                        <td><?php echo ++$i ?></td>
                                        <td><?php echo $row['category_name'] ?></td>
                                        <td><?php echo $row['product_name'] ?></td>
                                        <td><?php echo $row['qty_last'] ?></td>
                                    </tr>
                                    <?php $catTotal += $row['qty_last']; ?>
                                    <?php $count++; ?>
                                    <?php if ($count == count($rows)): ?>
                                        <tr>
                                            <td colspan="3"></td>
                                            <td><?php echo $catTotal ?></td>
                                        </tr>
                                    <?php endif; ?>
                                <?php endforeach; ?>
                            </tbody>
                        </table>
                    <?php endif; ?>
                </div>
            </div>
        </div>
    </div>
    <div class="span6">
        <div class="portlet box grey">
            <div class="portlet-title">
                <div class="caption"><i class="icon-reorder"></i>"<?php echo $stores[$storeId] ?>" салбарын үлдэгдэл</div>
                <div class="tools">
                    <a href="javascript:;" class="collapse"></a>
                </div>
            </div>
            <div class="portlet-body form">
                <div class="widget-box">
                    <form method="get" class="form-horizontal">
                        <div class="control-group">
                            <label class="control-label" for="store_id">Салбар:</label>
                            <div class="controls">
                                <select id="store_id" name="store_id" class="m-wrap">
                                    <?php foreach ($stores as $i => $v): ?>
                                        <option value="<?php echo $i ?>" <?php echo $i == $storeId ? 'selected="selected"' : ''; ?>><?php echo $v ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label" for="date">Огноо:</label>
                            <div class="controls">
                                <input type="text" name="date" id="date" class="m-wrap input-append date datepicker" data-date-format="yyyy-mm-dd" data-date-viewmode="years" readonly="true" value="<?php echo $date ?>">
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label" for="category_id">Ангилал:</label>
                            <div class="controls">
                                <select id="category_id" name="category_id" class="m-wrap">
                                    <option value="0">[Бүгд]</option>
                                    <?php foreach ($categories as $i => $v): ?>
                                        <option value="<?php echo $i ?>" <?php echo $i == $categoryId ? 'selected="selected"' : ''; ?>><?php echo $v ?></option>
                                    <?php endforeach; ?>
                                </select>
                            </div>
                        </div>

                        <div class="control-group">
                            <label class="control-label" for="product_id">Бараа:</label>
                            <div class="controls">
                                <select id="product_id" name="product_id" class="m-wrap">
                                    <option value="0">[Бүгд]</option>
                                </select>
                            </div>
                        </div>

                        <div class="form-actions">
                            <button type="submit" class="btn btn-primary">
                                <i class="icon-filter icon-white"></i> Шүүх
                            </button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>
</div>
<script>
    function categoryChanged() {
        c = $(this).val();
        if (c > 0) {
            $.ajax({
                url: '<?php echo $urlProduct ?>?category_id=' + c,
                dataType: 'json',
                success: function(json) {
                    if ($.isEmptyObject(json)) {
                        $('#product_id').html('<option value="0">[Ангилалд бараа алга]</option>');
                    } else {
                        $('#product_id').html('');
                        $('#product_id').html('<option value="0">[Бүгд]</option>');
                        $.each(json, function(i, v) {
                            $('#product_id').append('<option value="' + i + '">' + v + '</option>');
                        });
                    }
                }
            });
        } else {
            $('#product_id').html('<option value="0">[Бүгд]</option>');
        }
    }

    $(function() {
        $('#category_id').change(categoryChanged);
    });
</script>